package com.itheima.service;


import com.alibaba.fastjson.JSON;
import com.itheima.admin.categorypojo.Category;
import com.itheima.admin.AdminPage;
import com.itheima.admin.categorypojo.dto.CategoryDto;
import com.itheima.admin.categorypojo.vo.CategoryVo;
import com.itheima.mapper.admin.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;


/**
 * @author wuzhiyong
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 新增
     *
     * @param categoryDto categoryDto
     * @return 条数>0
     */
    public boolean addCategory(CategoryDto categoryDto) {
        redisTemplate.delete("adminPage");
        return categoryMapper.insertCategory(categoryDto) > 0;
    }

    /**
     * 分类分页查询
     *
     * @param page     页
     * @param pageSize 页数
     * @return employeePage
     */
    public AdminPage<CategoryVo> getPageCategory(Integer page, Integer pageSize) {

        String adminPageList = (String) redisTemplate.opsForHash().get("adminPage", page + "" + pageSize);

        if (adminPageList != null) {
            return JSON.parseObject(adminPageList, AdminPage.class);
        }

        //获取startindex
        int startIndex = (page - 1) * pageSize;
        //获取总条数
        int total = categoryMapper.selectAll();
        //获取分页后内容
        List<CategoryVo> selectpage = categoryMapper.selectPageCategory(startIndex, pageSize);
        //当查出内容为空时进入判断
        if (CollectionUtils.isEmpty(selectpage)) {
            //第一种情况 当前端查询一个不存在数据的页面时，默认返回第一页的数据
            page = 1;
            int startIndexNew = 0;
            List<CategoryVo> selectpagenew = categoryMapper.selectPageCategory(startIndexNew, pageSize);
            //当第一页也为空时，代表数据库中无数据
            if (CollectionUtils.isEmpty(selectpagenew)) {
                return null;
            }
            //将第一页数据封装到分页实体类中并返回
            AdminPage<CategoryVo> adminPage = new AdminPage<>();
            adminPage.setCurrent(page);
            adminPage.setSize(pageSize);
            adminPage.setTotal(total);
            adminPage.setRecords(selectpagenew);
            return adminPage;
        }
        //不为空则为第一次查询
        //组装实体类
        AdminPage<CategoryVo> adminPage = new AdminPage<>();
        adminPage.setCurrent(page);
        adminPage.setSize(pageSize);
        adminPage.setTotal(total);
        adminPage.setRecords(selectpage);
        //将mysql查出的数据缓冲到redis中
        redisTemplate.opsForHash().put("adminPage", page + "" + pageSize, JSON.toJSONString(adminPage));
        //返回实体类
        return adminPage;
    }

    /**
     * 删除
     *
     * @param id id
     * @return 条数>0
     */
    public boolean removeByIdCategory(String id) {
        redisTemplate.delete("adminPage");
        return categoryMapper.deleteByIdcg(id) > 0;
    }

    /**
     * 编辑
     *
     * @param categoryDto categoryDto
     * @return 条数>0
     */
    public boolean modifyByIdCategory(CategoryDto categoryDto) {
        redisTemplate.delete("adminPage");
        return categoryMapper.updateByIdCategory(categoryDto) > 0;
    }

    /**
     * 分类下拉列表
     *
     * @param type type
     * @return Category
     */
    public List<Category> getByTypeCategory(Integer type) {
        redisTemplate.delete("adminPage");
        return categoryMapper.selectByType(type);
    }
}
